package Interface;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;
import java.io.StringReader;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

import chrriis.dj.nativeswing.NativeSwing;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;

public class Main extends JFrame{
	
	public static String API_KEY = "103827286491232";  
	public static String SECRET = "758e5dec7578a573c5271f84072b19e9";  
	  
	public static String firstRequest = "https://graph.facebook.com/oauth/authorize?"  
	  + "client_id="  
	  + API_KEY  
	  + "&redirect_uri=http://www.facebook.com/connect/login_success.html&"  
	  + "scope=publish_stream,read_stream,email";  
	    
	public static String secondRequest="https://graph.facebook.com/oauth/access_token?"  
	  + "client_id="  
	  + API_KEY  
	  + "&redirect_uri=http://www.facebook.com/connect/login_success.html&"  
	  + "client_secret=" + SECRET + "&code=";  
	  
	public static String access_token = "";  
	public static boolean firstRequestDone = false;  
	public static boolean secondRequestDone = false;	
	
	public Main() throws IOException {		
		NativeSwing.initialize();
		NativeInterface.open();
		auth();
	}

	public void auth(){
	    JPanel webBrowserPanel = new JPanel(new BorderLayout());  
	    final JWebBrowser webBrowser = new JWebBrowser();					    
	    webBrowser.navigate(firstRequest);
	    webBrowser.addWebBrowserListener(new WebBrowserAdapter() {  
	      public void locationChanged(WebBrowserNavigationEvent e) {  
	        super.locationChanged(e);    
	        if (!firstRequestDone) {  
	          if (e.getNewResourceLocation().contains("http://www.facebook.com/connect/login_success.html?code=")){    
	            String[] splits = e.getNewResourceLocation().split("=");  
	            String stage2temp = secondRequest + splits[1];  
	            webBrowser.navigate(stage2temp);  
	            firstRequestDone = true;  
	          }
	        } else {  
	          if (!secondRequestDone) {  
	            System.out.println(webBrowser.getHTMLContent());  
	            StringReader readerSTR = new StringReader(webBrowser.getHTMLContent());    
	            HTMLEditorKit.ParserCallback callback =  new HTMLEditorKit.ParserCallback() {  
	              public void handleText(char[] data,int pos) {  	                  
	                String string = new String(data);  
	                String[] temp1 = string.split("&");  
	                String[] temp2 = temp1[0].split("=");  
	                access_token = temp2[1];  
	                System.out.println("Acess: "+access_token);
	                new Thread(){
	                	public void run(){
	                		new FaceAnimate(access_token);
	                	}
	                }.start();
	                setVisible(false);	                
	              }  
	            };  
	            try {   
	            	new ParserDelegator().parse(readerSTR, callback, false);  
	            } catch (IOException e1) {  
	            	e1.printStackTrace();  
	            }
	          }  
	        }  
	      }  
	    });  
	    webBrowserPanel.add(webBrowser,BorderLayout.CENTER);  
	    getContentPane().add(webBrowserPanel);  
	    setSize(400, 500);
	    setLocationRelativeTo(null);
	    setVisible(true);	    
	}

	@Override
	protected void finalize() throws Throwable {
		NativeInterface.runEventPump();
		super.finalize();
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					new Main();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}	

}
